package com.qjq.glasses_shopping.controller;


import com.qjq.glasses_shopping.result.BaseResult;
import com.qjq.glasses_shopping.service.FileService;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import java.io.IOException;


@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    private FileService fileService;

    /**
     * 文件上传
     * @param file 文件
     * @return 返回文件路径
     * @throws IOException
     */
    @PostMapping("/uploadImage")
    public BaseResult<String> upload(MultipartFile file) throws IOException {
        //因为file不能再服务之间传递
        byte[] bytes= file.getBytes();
        String url = fileService.uploadImage(bytes, file.getOriginalFilename());
        return BaseResult.ok(url);
    }

    /**
     * 删除文件
     * @param filePath 文件路径
     * @return 返回执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(String filePath) {
        fileService.delete(filePath);
        return BaseResult.ok();
    }
}
